<template>
  <div class="app-container">
    <div :ref="listQuery" :model="listQuery" class="filter-container">
      <el-input
        v-model="listQuery.filter"
        placeholder="关键字"
        class="filter-item"
        clearable
      />

      <el-button type="primary" icon="el-icon-search" @click="handleSearch"
        >查询</el-button
      >
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      style="width: 100%"
      height="400"
    >
      <el-table-column width="50" label="#">
        <template slot-scope="scope">
          <span>{{
            scope.$index + (listQuery.page - 1) * listQuery.limit + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="账号"></el-table-column>
      <el-table-column prop="vserName" label="真实名称"></el-table-column>
      <el-table-column prop="deptName" label="部门"></el-table-column>
      <el-table-column prop="loginTime" label="登录时间"></el-table-column>
      <el-table-column prop="ip" label="ip"></el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="kickout(scope.row)" type="text" size="small"
            >强退</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { onlineUser, kickout } from "@/api/monitor/monitor";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import { getDate, formatDateTime, offsetMinute } from "@/utils";
export default {
  name: "OnlineUser",
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        filter: ""
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      //If the Promise is rejected, the rejected value is thrown.
      try {
        const res = await onlineUser(this.listQuery);
        this.listLoading = false;
        this.list = res.result.rows;
        this.total = res.result.records;
      } catch (e) {
        this.listLoading = false;
      }
    },
    handleSearch() {
      this.getList();
    },
    kickout(row, column) {
      this.$confirm("确认要强制退出 [" + row.name + "] 用户吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await kickout(row.token);
          this.$message({
            message: "强制退出成功",
            type: "success"
          });
          this.getList();
        })
        .catch(err => {
          console.error(err);
        });
    }
  }
};
</script>
